package com.jxaummd.mushroom.main.common.presenter;


import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.jxaummd.mushroom.core.app.account.AccountManager;
import com.jxaummd.mushroom.core.base.presenter.BasePresenter;
import com.jxaummd.mushroom.core.util.net.RestClient;
import com.jxaummd.mushroom.core.util.tool.storage.UniversityPreference;
import com.jxaummd.mushroom.main.common.contract.LoginContract;

public class LoginPresenter extends BasePresenter<LoginContract.View>
        implements LoginContract.Presenter {

    public LoginPresenter(LoginContract.View view) {
        super(view);
    }

    @Override
    public void start() {
        super.start();
        getView().setNoStatusBar();
    }

    @Override
    public void login(String phoneNumber, String password) {
        RestClient.builder()
                .url("http://47.107.183.2/mushroom/client/login")
                .params("username", phoneNumber)
                .params("password", password)
                .success(response -> {
                    JSONObject jsonObject = JSONObject.parseObject(response);
                    if (((int) (jsonObject.get("status"))) == 200) {
                        AccountManager.setSignState(true);
                        UniversityPreference.addCustomAppProfile("user",jsonObject.getString("data"));
                        ToastUtils.showLong(jsonObject.getString("describe"));
                        getView().loginSuccess();
                    }

                })
                .build()
                .get();
    }

}
